<template>
  <div class="hot">
    <div class="top" v-if="playList">
      <div class="wrap">
        <img src="../assets/hot_music_icon.png" class="title" />
        <p>更新日期:{{ playList.updateTime | formateTime }}</p>
        <p class="desc">{{ playList.description | getDesc }}</p>
      </div>
    </div>
    <SongList
      :songs="showList"
      :showNum="true"
      :numLight="true"
      @play="$emit('play', $event)"
    ></SongList>
    <div class="moreBtn" v-if="!showLoading" @click="loadMore" ref="moreBtn">
      {{
        hotList.length > showList.length
          ? moreHint
            ? "松开加载更多"
            : "加载更多"
          : "到底了"
      }}
    </div>
    <Loading v-if="showLoading"></Loading>
  </div>
</template>


<script>
import SongList from "@/components/SongList";
import Loading from "@/components/Loading";
// import $ from "jquery";
export default {
  name: "Hot",
  data() {
    return {
      playList: null,
      hotList: [],
      showList: [],
      showNumber: true,
      limit: 20,
      showLoading: false,
      onTouch: false,
      moreHint: false,
    };
  },
  components: {
    SongList,
    Loading,
  },
  mounted() {
    let _this = this;
    let body = document.body;
    let html = document.documentElement;
    body.ontouchstart = function () {
      _this.onTouch = true;
    };
    body.ontouchend = function () {
      _this.onTouch = false;
      body.onscroll();
    };
    body.onscroll = function () {
      let moreBtn = _this.$refs.moreBtn;
      if (!moreBtn) return;
      var lh = parseInt(window.getComputedStyle(moreBtn).lineHeight);
      let st = Math.ceil(body.scrollTop || html.scrollTop);
      let max = body.clientHeight - window.innerHeight;
      let h = max - moreBtn.clientHeight + lh;
      _this.moreHint = false;
      if (st >= h) {
        if (st >= max - lh && _this.hotList.length > _this.showList.length) {
          _this.moreHint = true;
          if (!_this.onTouch) {
            _this.loadMore();
          }
        } else if (!_this.onTouch) {
          setTimeout(() => {
            html.scrollTop ? (html.scrollTop = h) : (body.scrollTop = h);
          }, 0);
        }
      }
    };
  },
  destroyed() {
    let body = document.body;
    body.onscroll = null;
    body.ontouchstart = null;
    body.ontouchend = null;
  },
  created() {
    this.showLoading = true;
    this.axios
      .get("/playlist/detail", {
        params: {
          id: 3778678,
        },
      })
      .then((d) => {
        // console.log(d)
        this.playList = d.data.playlist;
        this.hotList = d.data.playlist.tracks.map((item, index) => {
          item.song = {
            artists: item.ar,
            privilege: {
              playMaxbr: d.data.privileges[index].playMaxbr,
            },
            album: item.al,
            alias: item.alia,
          };
          return item;
        });
        this.showList = this.hotList.slice(0, this.limit);
        // console.log(this.showList);
        this.showLoading = false;
      });
  },
  methods: {
    loadMore() {
      this.showLoading = true;
      let num = Math.min(
        this.showList.length + this.limit,
        this.hotList.length
      );
      this.showList = this.hotList.slice(0, num);
      this.showLoading = false;
    },
  },
  filters: {
    formateTime(val) {
      let d = new Date(val);
      return d.getMonth() + 1 + "月" + d.getDate() + "日";
    },
    getDesc(val) {
      return String(val).replace(new RegExp("云音乐", "g"), "XX音乐");
    },
  },
};
</script>

<style lang="less" scoped>
.hot {
  padding: 0 15px;
  padding-top: 15px;
  .top {
    position: relative;
    padding-top: 38.9%;
    overflow: hidden;
    background: url(../assets/hot_music_bg.jpg) no-repeat;
    background-size: contain;
    border-radius: 1.867vw;
    margin-bottom: 15px;
    filter: hue-rotate(160deg);
    &:after {
      content: "";
      position: absolute;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
      z-index: 1;
      background-color: rgba(0, 0, 0, 0.2);
    }
    .wrap {
      display: flex;
      flex-direction: column;
      justify-content: center;
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      z-index: 2;
      padding-left: 20px;
      box-sizing: border-box;
      .title {
        width: 32vw;
      }
      p {
        margin-top: 10px;
        color: hsla(0, 0%, 100%, 0.8);
        font-size: 12px;
        transform: scale(0.91);
        transform-origin: left top;
        font-size: 3vw;
      }
      .desc {
        position: absolute;
        left: 50%;
        line-height: 2;
        font-size: 3vw;
      }
    }
  }
  .moreBtn {
    height: 220px;
    width: 100vw;
    margin-left: -15px;
    line-height: 55px;
    text-align: center;
    color: #999;
    position: relative;
    background: linear-gradient(#fcfcfd 50%, #eee 100%);
  }
}
</style>